from GA import GA, F
import numpy as np

config = {
    'function': lambda x: -F(x),
    'f_range': (-1, 2),
    'length': 22,
    'population_size': 30,
    'retain_rate': 0.5,
    'random_select_rate': 0.2,
    'mutation_rate': 0.5
}
ga = GA(config)


def test_init_population():
    print()
    population = np.array2string(ga.population, formatter={'all': lambda x: f'{x:#024b}'})
    print(population)


def test_get_selection():
    print()
    # population = np.array2string(ga.population, formatter={'all': lambda x: f'{x:#024b}'})
    parents = ga.get_selection()
    print(ga.population.shape, parents.shape)

    parents = np.array2string(parents, formatter={'all': lambda x: f'{x:#024b}'})
    print(parents)


def test_get_crossover():
    print()
    parents = ga.get_selection()
    print(parents.shape)
    children = ga.get_crossover(parents, n=ga.num - parents.shape[0])
    print(children.shape)
    children = np.array2string(children, formatter={'all': lambda x: f'{x:#024b}'})
    print(children)


def test_get_mutation():
    print()
    # population = np.array2string(ga.population, formatter={'all': lambda x: f'{x:#024b}'})
    # print(population)
    population = ga.get_mutation(ga.mutation_rate)
    # print(population.shape)
    # population = np.array2string(population, formatter={'all': lambda x: f'{x:#024b}'})
    # print(population)
    # sum = 0
    for i in range(1):
        count = 0
        population = ga.get_mutation(ga.mutation_rate)
        for a, b in zip(ga.population, population):
            a = np.array2string(a, formatter={'all': lambda x: f'{x:#024b}'})
            b = np.array2string(b, formatter={'all': lambda x: f'{x:#024b}'})
            if a != b:
                # print([a, b])
                count += 1
                # sum += 1
        print('count:', count)
